package com.me.dao.impl;

import com.me.dao.IVisitDao;
import com.me.domain.Visit;
import com.me.util.DBHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class VisitDao implements IVisitDao {
	@Override
	public List<Visit> getVisitsByPetId(int petId) {
		try {
			QueryRunner runner = new QueryRunner(DBHelper.getDatasource());
			String sql = "select v.* from visit v inner join vet on (v.vetId=vet.id) where v.petId=?";
			return runner.query(sql, new BeanListHandler<Visit>(Visit.class), petId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void save(Visit visit) {
		try {
			QueryRunner runner = new QueryRunner(DBHelper.getDatasource());
			String sql = "insert into visit value(null,?,?,CURDATE(),?,?)";
			runner.update(sql,visit.getPetId(),visit.getVetId(),visit.getDescription(),visit.getTreatment());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void del(int id) {
		try {
			QueryRunner runner = new QueryRunner(DBHelper.getDatasource());
			String sql = "DELETE FROM `pet`.`visit` WHERE (`id` = ?);";
			runner.update(sql,id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
